class Admin::LicensesController < ApplicationController
  # GET /admin_licenses
  def index
    @rally_licenses = License.find(:all, :conditions => ['kind = ?', 'rajdowa'])
    @referee_licenses = License.find(:all, :conditions => ['kind = ?', 'sędziowska'])
  end
  
  # GET /admin_licenses/1
  def show
    @license = License.find(params[:id])

    respond_to do |format|
      format.html # show.html.erb
      format.xml  { render :xml => @license }
    end
  end

  # GET /admin_licenses/new
  def new
    #ustawiamy licencje i kategorie lic if juz byla - np gdy przyslana z powrotem z akji update
    @license = params[:license] ? License.new(params[:license]) : License.new
    respond_to do |format|
      format.html # new.html.erb
      format.xml  { render :xml => @license }
    end
  end

  # GET /admin_licenses/1/edit
  def edit
    @license = License.find(params[:id])
  end

  # POST /admin_licenses
  def create
    @license = License.new(params[:license])
    respond_to do |format|
      if @license.save
        flash[:notice] = 'Dodano licencję.'
        format.html { redirect_to([:admin, @license]) }
        format.xml  { render :xml => @license, :status => :created, :location => @license }
      else
        flash[:error] = 'Musisz wypełnić wszystkie pola!'
        format.html { redirect_to :action => "new", :license => params[:license] }
        format.xml  { render :xml => @license.errors, :status => :unprocessable_entity }
      end
    end
  end

  # PUT /admin_licenses/1
  def update
    @license = License.find(params[:id])
    
    respond_to do |format|
      if @license.update_attributes(params[:license])
        flash[:notice] = 'Zapisano zmiany.'
        format.html { redirect_to([:admin, @license]) }
        format.xml  { head :ok }
      else
        flash[:error] = 'Musisz wypełnić wszystkie pola!'
        format.html { redirect_to :action => "edit", :license => params[:license] }
        format.xml  { render :xml => @license.errors, :status => :unprocessable_entity }
      end
    end
  end

  # DELETE /admin_licenses/1
  def destroy
    @license = License.find(params[:id])
    @license.destroy

    respond_to do |format|
      format.html { redirect_to(admin_licenses_url) }
      format.xml  { head :ok }
    end
  end
end